package com.xxx.springboot.pojo;

import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import lombok.Data;

@Data
public class Result {
	
	static int RESULT_SUCCESS = 200;
	static int RESULT_ERROR = 100;

	private int code;
	private String msg;
	private Object data;

	private Result(int code, String msg, Object data) {
		this.code = code;
		this.msg = msg;
		this.data = data;
	}

	private Result(int code, String msg) {
		this.code = code;
		this.msg = msg;
	}

	public static String ok() {
		return JSONObject.toJSONString(new Result(RESULT_SUCCESS, "success"), SerializerFeature.WriteMapNullValue);
	}

	public static String ok(Object data) {
		return JSONObject.toJSONString(new Result(RESULT_SUCCESS, "success", data), SerializerFeature.WriteMapNullValue);
	}

	public static Result okObj(Object data) {
		return new Result(RESULT_SUCCESS, "success", data);
	}

	public static Result okObj() {
		return new Result(RESULT_SUCCESS, "success");
	}

	public static String fail() {
		return JSONObject.toJSONString(new Result(RESULT_ERROR, "error", null), SerializerFeature.WriteMapNullValue);
	}

	public static String fail(String msg) {
		return JSONObject.toJSONString(new Result(RESULT_ERROR, msg, null), SerializerFeature.WriteMapNullValue);
	}

	public static String fail(int code, String msg) {
		return JSONObject.toJSONString(new Result(code, msg, null), SerializerFeature.WriteMapNullValue);
	}

	public static Result failObj(int code, String msg) {
		return new Result(code, msg, null);
	}

	public static String result(int code, String msg, Map<String, Object> data) {

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("code", code);
		jsonObject.put("msg", msg);
		jsonObject.put("data", data);
		return jsonObject.toJSONString();
	}

	public static String result(int code, String msg) {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("code", code);
		jsonObject.put("msg", msg);
		return jsonObject.toJSONString();
	}

	public static String result(int code, Map<String, Object> data) {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("code", code);
		jsonObject.put("data", data);
		return jsonObject.toJSONString();
	}
}
